package cn.shop.dys.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

import cn.shop.dys.R;

/**
 * 带头和尾的lv
 * Created by sunjian on 2016/4/15.
 */
public class HeadFootListView extends ListView implements AbsListView.OnScrollListener {
    private int downY;
    private View headView;
    private View footView;
    private int footHeight;
    private int headHeight;
    private boolean isHeadStart;
    private int firstVisibleItem;
    private boolean isFootStart;

    public HeadFootListView(Context context) {
        this(context, null);
    }

    public HeadFootListView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HeadFootListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        headView = View.inflate(getContext(), R.layout.head, null);
        footView = View.inflate(getContext(), R.layout.foot, null);
        headView.measure(0, 0);
        footView.measure(0, 0);
        footHeight = footView.getMeasuredHeight();
        headHeight = headView.getMeasuredHeight();
        addHeaderView(headView);
        addFooterView(footView);
        headView.setPadding(0, -headHeight, 0, 0);
        footView.setPadding(0, 0, 0, -footHeight);
        setOnScrollListener(this);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (firstVisibleItem == 0 && !isHeadStart) {
                    isHeadStart = true;
                    downY = (int) ev.getY();
                }
                if (isFootStart) {
                    downY = (int) ev.getY();
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (firstVisibleItem == 0 && !isHeadStart) {
                    isHeadStart = true;
                    downY = (int) ev.getY();
                }
                if (isHeadStart) {
                    int moveHeight = (int) ev.getY();
                    int dY = moveHeight - downY;
                    headView.setPadding(0, -headHeight + dY, 0, 0);
                   /* if(headView.getPaddingTop()>-headHeight){
                        return true;
                    }*/
                }
                if (isFootStart) {
                    int moveHeight = (int) ev.getY();
                    int dY = moveHeight - downY;
                    footView.setPadding(0, 0, 0, -footHeight - dY);
                }
                break;
            case MotionEvent.ACTION_UP:
                if (isHeadStart) {
                    headView.setPadding(0, -headHeight, 0, 0);
                    isHeadStart = false;
                }
                if (isFootStart) {
                    footView.setPadding(0, 0, 0, -footHeight);
                    isFootStart = false;
                }
                break;
        }
        return super.onTouchEvent(ev);
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        this.firstVisibleItem = firstVisibleItem;
        if (view.getLastVisiblePosition() == (view.getCount() - 1)) {
            isFootStart = true;
        } else {
            isFootStart = false;
        }
    }
}
